package com.jml.loadbalancer.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import com.jml.loadbalancer.LoadBalancer;

/**
 * @Description:轮询算法
 */
public class RotationLoadBalancer implements LoadBalancer<String> {
    
    private int index;
    private AtomicLong atomicLong = new AtomicLong(0);

    /**
     * 轮训机制 算法1
     *
     * @param addres
     * @return
     */
    public synchronized String getSingleAddres001(List<String> addres) {
        if (index >= addres.size()) {
            index = 0;
        }
        String ads = addres.get(index++);
        System.out.println("ads:" + ads);
        return ads;
    }
    /**
     * @Description:取模
     * @param addres
     * @return:java.lang.String
     */
    public String getSingleAddres(List<String> addres) {
        Long index = atomicLong.incrementAndGet() % addres.size();
        String ads = addres.get(Integer.parseInt(index + ""));
        System.out.println("ads:" + ads);
        return ads;
    }
}
